<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 创建订阅对象
        const event = {
            clientList: [],
            listen(key, fn) {
                if (!this.clientList[key]) {
                    this.clientList[key] = [];
                }
                this.clientList[key].push(fn);
            },
            trigger() {
                let key = Array.prototype.shift.call(arguments),
                    fns = this.clientList[key];

                if (!fns || fns.length === 0) {
                    return false
                }

                for (let i = 0, fn; fn = fns[i++];) {
                    fn.apply(this, arguments);
                }
            }
        }

        const installEvent = function (obj) {
            for (const key in event) {
                obj[key] = event[key];
            }
        }

        // 取消订阅
        event.remove = function (key, fn) {
            let fns = this.clientList[key];
            if (!fns) {
                return false;
            }
            if (!fn) {
                fns && (fns.length = 0);
            } else {
                for (let i = fns.length - 1; i >= 0; i--) {
                    let _fn = fns[i];
                    if (_fn === fn) {
                        fns.splice(i, 1);
                    }
                }
            }
        }

        // 测试用例
        let salesOffices = {};

        installEvent(salesOffices);


        salesOffices.listen('squareMeter88', fn1 = function (price) {
            console.log('价格= ' + price);
        });
        salesOffices.listen('squareMeter88', fn2 = function (price) {
            console.log('价格= ' + price);
        });
        salesOffices.remove('squareMeter88', fn1);
        salesOffices.trigger('squareMeter88', 2000000);
    </script>
</body>

</html>